<?php

namespace app\base\controller\api;

use app\base\service\MemberService;
use app\base\service\WsSendService;
use think\admin\Controller;
use app\base\model\RedianTask;
use app\base\model\RedianData;
use app\base\controller\api\Auth;
use app\base\service\TophubdataService;
use app\base\service\AlibailianService;
use app\base\service\HeygenService;
use app\base\service\HuoshanService;
use app\base\service\MinimaxService;
use app\base\service\SimilarityService;
use app\base\service\TaskService;

/**
 * 任务接口
 * Class Login
 * @package app\store\controller
 */
class Task extends Auth
{
    //统计任务数据
    public function count()
    {
        //总任务数
        $count = RedianTask::mk()->where(['user_id'=>$this->user_id,'is_deleted'=>0])->count();
        //已完成任务数
        $complete_count = RedianTask::mk()->where(['user_id'=>$this->user_id,'is_deleted'=>0])->where('progress',7)
            ->whereIn('type',"2,3")->count();
        $complete_count2 = RedianTask::mk()->where(['user_id'=>$this->user_id,'is_deleted'=>0,'status'=>0])->whereIn('type',"1,4")->count();
        //执行中任务数
        $doing_count = RedianTask::mk()->where(['user_id'=>$this->user_id,'is_deleted'=>0])->where('progress',"<>",7)
            ->whereIn('type',"2,3")
            ->where('progress',"<>",0)->where('error_status',0)->count();
        $doing_count2 = RedianTask::mk()->where(['user_id'=>$this->user_id,'is_deleted'=>0,'status'=>1])->whereIn('type',"1,4")->count();
        $this->success('获取成功', ['count'=>$count,'complete_count'=>$complete_count+$complete_count2,'doing_count'=>$doing_count+$doing_count2]);

    }

    //API配置校验
    public function apiConfigCheck()
    {
        $user = $this->app->db->name('ai_user')->where(['id'=>$this->user_id])->find();
        if(empty($user)){
            $this->error('用户不存在');
        }
        if(time()>strtotime($user['expire_date'])){
            $this->error('账号已过期,请先续费',null,4);
        }
        if(MemberService::instance()->getUserRemainingBeans($this->user_id)<2){
            $this->error('账户余额不足,请先充值',null,5);
        }


        $apiConfig = $this->app->db->name('ai_api_config')->where(['user_id'=>$this->user_id])->column('type');
       // dump($this->user);
        if($this->user['api_config_type']==1){
//            if(!in_array(8,$apiConfig)){
//                $this->error('请先配置蝉镜 API参数');
//            }
//            if(!in_array(4,$apiConfig)){
//                $this->error('请先配置HeyGen API参数');
//            }
        }else{
            if(empty($apiConfig)){
                $this->error('请先配置API参数');
            }else if(!in_array(1,$apiConfig)){
                $this->error('请先配置榜眼数据API参数');
            }else if(!in_array(2,$apiConfig)){
                $this->error('请先配置阿里百炼API参数');
            }else if(!in_array(4,$apiConfig)){
                $this->error('请先配置HeyGen API参数');
            }else if(!in_array(6,$apiConfig)) {
                $this->error('请先配置MinMax API参数');
            }
        }
        $voice = $this->app->db->name('ai_voice')->where(['user_id'=>$this->user_id,'type'=>1])->count();
        if($voice<=0){
            $this->error('请先配置音频模板',null,2);
        }
        $video = $this->app->db->name('ai_voice')->where(['user_id'=>$this->user_id,'type'=>2])->count();
        if($video<=0){
            $this->error('请先配置视频模板',null,3);
        }

        $this->success('校验成功');

    }

    //创建采集任务
    public function create()
    {
        $post_data = $this->app->request->post();
        $save_data = [
            'title'=>$post_data['taskName'],
            'keywords'=>$post_data['keywords'],
            'num'=>$post_data['count'],
            'code'=>"TD".date('YmdHis').rand(1000,9999),
            'system_prompt'=>$post_data['systemPrompt'],
            'user_prompt'=>$post_data['userPrompt'],

            'audio_platform'=>$post_data['selectedAudioPlatform'],
            'voice'=>$post_data['selectedVoice'],
            'voice_model'=>$post_data['selectedVoiceModel'],
            'video_platform'=>$post_data['selectedVideoPlatform'],
            'video'=>$post_data['selectedVideo'],
            'text_select'=>$post_data['textSelect'],
            'user_id'=>$this->user_id,
            'type'=>$post_data['type'],
            'is_auto_fb'=>$post_data['is_auto_fb'],
            'is_auto_jjjb'=>$post_data['is_auto_jjjb'],
            'is_auto_fbjb'=>$post_data['is_auto_fbjb'],
            'schedule_value'=>$post_data['schedule_value']*60,
            'total_count'=>$post_data['total_count'],
            'star_time'=>$post_data['date_ragne'][0],
            'end_time'=>$post_data['date_ragne'][1],
            'status'=>1,
        ];
        if(isset($post_data['zdy_video_keywords'])){
            $save_data['zdy_video_keywords'] = $post_data['zdy_video_keywords'];
        }
        if(isset($post_data['schedule']) && $post_data['is_auto_fb']==1){
            //根据设备数量计算每个时段的产生数量
            $schedule = $post_data['schedule'];
            $out_num = 0;
            foreach ($schedule as $v){
                $out_num+=count($v['target_device']);
            }
            if($out_num==0){
                $this->error('请选择设备');
            }
            $save_data['out_num'] = $out_num;

            $save_data['push_schedule'] = json_encode($post_data['schedule']);
            $save_data['schedule_task'] = json_encode($post_data['schedule_task']);
        }else{
           // $save_data['schedule_task'] = '{"schedule_type":2,"schedule_value":1,"schedule_time":[{"time":["00:00:00","01:00:00"],"num":1,"out_num":1}],"num":1,"out_num":1,"date_ragne":["08:00:00","22:00:00"],"total_count":1}';
            $save_data['out_num']=$post_data['outCount'];


        }
        //dump($save_data);exit;
        if(isset($post_data['id']) && $post_data['id']>0){
            $task_id = RedianTask::mQuery()->where(['id'=>$post_data['id']])->update($save_data);
        }else{
            $task_id = RedianTask::mk()->insertGetId($save_data);
            //首次创建不自动发布的任务直接执行任务
            if($post_data['is_auto_fb']==2){
                $this->runTask($task_id);
            }
        }

        /*
        if(isset($post_data['schedule']) && $post_data['is_auto_fb']==1){
            $schedule = $post_data['schedule'];
            foreach ($schedule as $v){
                $run_script = [];
                if($post_data['is_auto_jjjb']==1){
                    $run_script[]=$this->app->db->name("script_confg")->where('type',0)->value('id');
                }
                if($post_data['is_auto_fbjb']==1){
                    $run_script[]=$this->app->db->name("script_confg")->where('type',$v['platform'])->value('id');
                }
                if($v['schedule_type']==2){
                    foreach ($v['target_device'] as $v2){
                        $save_schedule = [
                            'schedule_value'=>$post_data['schedule_value']*60,
                            'schedule_type'=>2,
                            'push_num'=>$v['push_num'],
                            'target_device'=>$v2,
                            'task_id'=>$task_id,
                            'user_id'=>$this->user_id,
                            'platform'=>$v['platform'],
                            'status'=>0,
                            'is_auto_jjjb'=>$post_data['is_auto_jjjb'],
                            'is_auto_fbjb'=>$post_data['is_auto_fbjb'],
                            'run_script'=>json_encode($run_script)

                        ];
                        $this->app->db->name('redian_push_schedule')->insert($save_schedule);
                    }
                }else{
                    foreach ($v['target_device'] as $v2){
                        foreach ($v['schedule_time'] as $v3){
                            $save_schedule = [
                                'schedule_value'=>implode(',',$v3['time']),
                                'schedule_type'=>$v['schedule_type'],
                                'push_num'=>$v3['push_num'],
                                'target_device'=>$v2,
                                'task_id'=>$task_id,
                                'user_id'=>$this->user_id,
                                'platform'=>$v['platform'],
                                'status'=>0,
                                'is_auto_jjjb'=>$post_data['is_auto_jjjb'],
                                'is_auto_fbjb'=>$post_data['is_auto_fbjb'],
                                'run_script'=>json_encode($run_script)
                            ];
                            $this->app->db->name('redian_push_schedule')->insert($save_schedule);
                        }
                    }
                }
            }
        }*/

       // dump($schedule);exit;

        $this->success('创建成功',$task_id);
    }


    //创建分享链接任务
    public function createTaskUrl()
    {
        $post_data = $this->app->request->post();
        $save_data = [
            'title'=>$post_data['taskName'],
            'code'=>"TD".date('YmdHis').rand(1000,9999),
            'system_prompt'=>$post_data['systemPrompt'],
            'user_prompt'=>$post_data['userPrompt'],
            'audio_platform'=>$post_data['selectedAudioPlatform'],
            'voice'=>$post_data['selectedVoice'],
            'voice_model'=>$post_data['selectedVoiceModel'],
            'video_platform'=>$post_data['selectedVideoPlatform'],
            'video'=>$post_data['selectedVideo'],
            'user_id'=>$this->user_id,
            'type'=>$post_data['type'],
            'is_auto_fb'=>1,
            'is_auto_jjjb'=>1,
            'is_auto_fbjb'=>1,
            'status'=>1,
        ];
        if(isset($post_data['zdy_video_keywords'])){
            $save_data['zdy_video_keywords'] = $post_data['zdy_video_keywords'];
        }
        $save_data['share_urls'] = json_encode([$post_data['share_urls']]);
        if(isset($post_data['schedule'])){
            $save_data['push_schedule'] = json_encode($post_data['schedule']);
        }
        $task_id = RedianTask::mk()->insertGetId($save_data);
        if(isset($post_data['schedule'])){
            $schedule = $post_data['schedule'];
            $total_devices_num = 0;
            foreach ($schedule as $v){
                $total_devices_num+=count($v['target_device']);
            }
            if($total_devices_num==0){
                $this->error('请选择设备');
            }
            foreach ($schedule as $v){
                $run_script = [];
                if($save_data['is_auto_jjjb']==1){
                    $run_script[]=$this->app->db->name("script_confg")->where('type',0)->value('id');
                }
                if($save_data['is_auto_fbjb']==1){
                    $jb_ids=$this->app->db->name("script_confg")->whereIn('type',$v['platform'])->column('id');
                    foreach ($jb_ids as $jb_id){
                        $run_script[]=$jb_id;
                    }
                }
                foreach ($v['target_device'] as $v2){
                    $save_schedule = [
                        'schedule_value'=>60,
                        'schedule_type'=>2,
                        'push_num'=>1,
                        'target_device'=>$v2,
                        'task_id'=>$task_id,
                        'user_id'=>$this->user_id,
                        'platform'=>$v['platform'],
                        'status'=>0,
                        'is_auto_jjjb'=>$save_data['is_auto_jjjb'],
                        'is_auto_fbjb'=>$save_data['is_auto_fbjb'],
                        'run_script'=>json_encode($run_script)
                    ];
                    $this->app->db->name('redian_push_schedule')->insert($save_schedule);
                }
            }
        }
        // dump($schedule);exit;
        $code = sysqueue("分享链接转换任务", "xbase:TaskRunUrl", $later = 1, $data = [
            'task_id'=>$task_id
        ], $rscript = 1, $loops = 0);
        $this->success('创建成功',$task_id);
    }




    //解析自定义口播文本
    public function parseExcel()
    {
        $file_url = $this->app->request->post('file_url');
        $excel_data = TaskService::instance()->parseExcel($file_url);
        if(empty($excel_data)){
            $this->error('解析失败');
        }
        if(!is_array($excel_data)){
            $this->error($excel_data);
        }
        unset($excel_data[0]);
        $data = array_values($excel_data);
        $this->success("解析成功,共解析出".count($data)."条数据",$data);

    }

    //创建自定义口播文案任务
    public function createZdy()
    {
        $post_data = $this->app->request->post();
        $save_data = [
            'title'=>$post_data['taskName'],
            'code'=>"TD".date('YmdHis').rand(1000,9999),
            'audio_platform'=>$post_data['selectedAudioPlatform'],
            'voice'=>$post_data['selectedVoice'],
            'voice_model'=>$post_data['selectedVoiceModel'],
            'video_platform'=>$post_data['selectedVideoPlatform'],
            'video'=>$post_data['selectedVideo'],
            'text_select'=>$post_data['textSelect'],
            'type'=>$post_data['type'],
            'user_id'=>$this->user_id,
            'file_path'=>$post_data['file_path'],
            'is_auto_fb'=>$post_data['is_auto_fb'],
            'is_auto_jjjb'=>$post_data['is_auto_jjjb'],
            'is_auto_fbjb'=>$post_data['is_auto_fbjb'],
        ];
        if(isset($post_data['schedule']) && $post_data['is_auto_fb']==1){
            $save_data['push_schedule'] = json_encode($post_data['schedule']);
        }
        $task_id = RedianTask::mk()->insertGetId($save_data);
        $excel_data = TaskService::instance()->parseExcel($post_data['file_path']);
        if(empty($excel_data)){
            $this->error('解析失败');
        }
        if(!is_array($excel_data)){
            $this->error($excel_data);
        }
        unset($excel_data[0]);
        $data = array_values($excel_data);
        $num = 0;
        foreach ($data as $v){
            $save_data = [
                'task_id'=>$task_id,
                'user_id'=>$this->user_id,
                'video_title'=>$v[0],
                'video_content'=>$v[1],
                'video_keywords'=>$v[2],
                'ai_gaixie_content'=>$v[3],
                'emotion'=>$v[4],
                'gaixie_status'=>2,
                'filter_status'=>1,
                'push_time'=>"1970-01-01 00:00:00",
                'is_auto_fb'=>$save_data['is_auto_fb'],
                'is_auto_jjjb'=>$save_data['is_auto_jjjb'],
                'is_auto_fbjb'=> $save_data['is_auto_fbjb'],
            ];
            RedianData::mk()->insert($save_data);
            $num++;
        }



        if(isset($post_data['schedule']) && $post_data['is_auto_fb']==1){
            $schedule = $post_data['schedule'];
            $total_devices_num = 0;
            foreach ($schedule as $v){
                $total_devices_num+=count($v['target_device']);
            }
            if($total_devices_num==0){
                $this->error('请选择设备');
            }



            foreach ($schedule as $v){
                $run_script = [];
                $total_devices = count($v['target_device']);
                if($total_devices==0){
                    continue;
                }
                // 计算分配基数
                $base = floor($num / $total_devices);
                $remainder = $num % $total_devices;
                $device_index = 0;
                if($post_data['is_auto_jjjb']==1){
                    $run_script[]=$this->app->db->name("script_confg")->where('type',0)->value('id');
                }
                if($post_data['is_auto_fbjb']==1){
                    $jb_ids=$this->app->db->name("script_confg")->whereIn('type',$v['platform'])->column('id');
                    foreach ($jb_ids as $jb_id){
                        $run_script[]=$jb_id;
                    }
                  //  $run_script[]=$this->app->db->name("script_confg")->where('type',$v['platform'])->value('id');
                }
                if($v['schedule_type']==2){

                    foreach ($v['target_device'] as $v2){
                        if ($post_data['is_auto_jjjb'] == 1) {
                            $push_num = $base;
                            if ($device_index < $remainder) {
                                $push_num += 1;
                            }
                            $device_index++;
                        } else {
                            $push_num = $v['push_num'];
                        }
                        $save_schedule = [
                            'schedule_value'=>$v['schedule_value']*60,
                            'schedule_type'=>$v['schedule_type'],
                            'push_num'=>$push_num,
                            'target_device'=>$v2,
                            'task_id'=>$task_id,
                            'user_id'=>$this->user_id,
                            'platform'=>$v['platform'],
                            'status'=>0,
                            'is_auto_jjjb'=>$post_data['is_auto_jjjb'],
                            'is_auto_fbjb'=>$post_data['is_auto_fbjb'],
                            'run_script'=>json_encode($run_script)
                        ];
                        $this->app->db->name('redian_push_schedule')->insert($save_schedule);
                    }
                }else{
                    foreach ($v['target_device'] as $v2){
                        foreach ($v['schedule_time'] as $v3){
                            $save_schedule = [
                                'schedule_value'=>implode(',',$v3['time']),
                                'schedule_type'=>$v['schedule_type'],
                                'push_num'=>$v3['push_num'],
                                'target_device'=>$v2,
                                'task_id'=>$task_id,
                                'user_id'=>$this->user_id,
                                'platform'=>$v['platform'],
                                'status'=>0,
                                'is_auto_jjjb'=>$post_data['is_auto_jjjb'],
                                'is_auto_fbjb'=>$post_data['is_auto_fbjb'],
                                'run_script'=>json_encode($run_script)
                            ];
                            $this->app->db->name('redian_push_schedule')->insert($save_schedule);
                        }
                    }
                }
            }
        }

        // dump($schedule);exit;

        $this->success('创建成功',$task_id);
    }


    //创建对标账号任务
    public function createAccount()
    {
        $post_data = $this->app->request->post();
        $save_data = [
            'title'=>$post_data['taskName'],
            'code'=>"TD".date('YmdHis').rand(1000,9999),
            'system_prompt'=>$post_data['systemPrompt'],
            'user_prompt'=>$post_data['userPrompt'],
            'audio_platform'=>$post_data['selectedAudioPlatform'],
            'voice'=>$post_data['selectedVoice'],
            'voice_model'=>$post_data['selectedVoiceModel'],
            'video_platform'=>$post_data['selectedVideoPlatform'],
            'video'=>$post_data['selectedVideo'],
            'text_select'=>$post_data['textSelect'],
            'user_id'=>$this->user_id,
            'type'=>$post_data['type'],
            'is_auto_fb'=>$post_data['is_auto_fb'],
            'is_auto_jjjb'=>$post_data['is_auto_jjjb'],
            'is_auto_fbjb'=>$post_data['is_auto_fbjb'],
            'schedule_value'=>$post_data['schedule_value']*60,
            'total_count'=>$post_data['total_count'],
            'schedule_type'=>$post_data['schedule_type'],
            'star_time'=>$post_data['date_ragne'][0],
            'end_time'=>$post_data['date_ragne'][1],
            'status'=>1,
            'home_page'=>$post_data['home_page']
        ];
        if(isset($post_data['zdy_video_keywords'])){
            $save_data['zdy_video_keywords'] = $post_data['zdy_video_keywords'];
        }
        if(isset($post_data['schedule'])){
            if($save_data['is_auto_fb']==1){
                $schedule = $post_data['schedule'];
                $total_devices_num = 0;
                foreach ($schedule as $v){
                    $total_devices_num+=count($v['target_device']);
                }
                if($total_devices_num==0){
                    $this->error('请选择设备');
                }
            }
            $save_data['push_schedule'] = json_encode($post_data['schedule']);;
        }
        if(isset($post_data['schedule_task'])){
            $save_data['schedule_task'] = json_encode($post_data['schedule_task']);
        }
        //dump($save_data);exit;
        if(isset($post_data['id']) && $post_data['id']>0){
            $task_id = RedianTask::mQuery()->where(['id'=>$post_data['id']])->update($save_data);
        }else{
            $task_id = RedianTask::mk()->insertGetId($save_data);
            if($save_data['schedule_type']==2){
                //首次创建按间隔时长的任务直接运行一次
                $task = RedianTask::mQuery()->where(['id'=>$task_id])->find();
                $task_run_log_id = $this->app->db->name("task_run_log")->insertGetId([
                    'task_id'=>$task['id'],
                    'num'=>$task['count']+1,
                    'user_id'=>$task['user_id'],
                    'title'=>$task['title'],
                ]);
//                if($task['push_schedule'] && $task['is_auto_fb']==1){
//                    $schedule = json_decode($task['push_schedule'],true);
//                    foreach ($schedule as $v){
//                        $run_script = [];
//                        if($task['is_auto_jjjb']==1){
//                            $run_script[]=$this->app->db->name("script_confg")->where('type',0)->value('id');
//                        }
//                        if($task['is_auto_fbjb']==1){
//                            $run_script[]=$this->app->db->name("script_confg")->where('type',$v['platform'])->value('id');
//                        }
//                        if($v['schedule_type']==2){
//                            foreach ($v['target_device'] as $v2){
//                                $save_schedule = [
//                                    'schedule_value'=>1,
//                                    'schedule_type'=>$v['schedule_type'],
//                                    'push_num'=>1,
//                                    'target_device'=>$v2,
//                                    'task_id'=>$task['id'],
//                                    'user_id'=>$task['user_id'],
//                                    'platform'=>$v['platform'],
//                                    'status'=>0,
//                                    'is_auto_jjjb'=>$task['is_auto_jjjb'],
//                                    'is_auto_fbjb'=>$task['is_auto_fbjb'],
//                                    'run_script'=>json_encode($run_script)
//                                ];
//                                $this->app->db->name('redian_push_schedule')->insert($save_schedule);
//                            }
//                        }else{
//                            foreach ($v['target_device'] as $v2){
//                                foreach ($v['schedule_time'] as $v3){
//                                    $save_schedule = [
//                                        'schedule_value'=>implode(',',$v3['time']),
//                                        'schedule_type'=>$v['schedule_type'],
//                                        'push_num'=>$v3['push_num'],
//                                        'target_device'=>$v2,
//                                        'task_id'=>$task['id'],
//                                        'user_id'=>$task['user_id'],
//                                        'platform'=>$v['platform'],
//                                        'status'=>0,
//                                        'is_auto_jjjb'=>$task['is_auto_jjjb'],
//                                        'is_auto_fbjb'=>$task['is_auto_fbjb'],
//                                        'run_script'=>json_encode($run_script)
//                                    ];
//                                    $this->app->db->name('redian_push_schedule')->insert($save_schedule);
//                                }
//                            }
//                        }
//                    }
//                }
                RedianTask::mQuery()->where([ "id"=>$task['id']])->inc('count')
                    ->update([
                        'error_status'=>0,
                        'progress'=>0,
                        'status'=>3,
                        'last_push'=>date('Y-m-d H:i:s')
                    ]);
                $code = sysqueue($task['title']."对标账号转换任务", "xbase:TaskRunAccount", $later = 1, $data = [
                    'task_id'=>$task['id'],'task_run_log_id'=>$task_run_log_id
                ], $rscript = 1, $loops = 0);
            }

        }
        $this->success('创建成功',$task_id);
    }



    public function getList()
    {
        $list = RedianTask::mk()->where(['user_id'=>$this->user_id,'is_deleted'=>0])
            ->order('id desc')->select()->toArray();
        foreach ($list as &$v){
            // 将多行文本按换行符分割成数组
            $remarkLines = explode("\n", $v['keywords']);
            // 去除每一行的前后空格
            $remarkLines = array_map('trim', $remarkLines);
            // 去除空行
            $remarkLines = array_filter($remarkLines, function($line) {
                return !empty($line);
            });
            $v['keywords'] = $remarkLines;
        }
        $this->success('获取成功', $list);

    }


    //初始化运行任务
    public function runTask($task_id)
    {
        $task = RedianTask::mk()->where(['user_id'=>$this->user_id,"id"=>$task_id])->find();
        $task_run_log_id = $this->app->db->name("task_run_log")->insertGetId([
            'task_id'=>$task['id'],
            'num'=>$task['count']+1,
            'user_id'=>$task['user_id'],
            'title'=>$task['title'],
        ]);
        if($task['push_schedule'] && $task['is_auto_fb']==1){
            $schedule = json_decode($task['push_schedule'],true);
            foreach ($schedule as $v){
                $run_script = [];
                if($task['is_auto_jjjb']==1){
                    $run_script[]=$this->app->db->name("script_confg")->where('type',0)->value('id');
                }
                if($task['is_auto_fbjb']==1){
                    $run_script[]=$this->app->db->name("script_confg")->where('type',$v['platform'])->value('id');
                }
                if($v['schedule_type']==2){
                    foreach ($v['target_device'] as $v2){
                        $save_schedule = [
                            'schedule_value'=>1,
                            'schedule_type'=>$v['schedule_type'],
                            'push_num'=>1,
                            'target_device'=>$v2,
                            'task_id'=>$task['id'],
                            'user_id'=>$task['user_id'],
                            'platform'=>$v['platform'],
                            'status'=>0,
                            'is_auto_jjjb'=>$task['is_auto_jjjb'],
                            'is_auto_fbjb'=>$task['is_auto_fbjb'],
                            'run_script'=>json_encode($run_script)
                        ];
                        $this->app->db->name('redian_push_schedule')->insert($save_schedule);
                    }
                }else{
                    foreach ($v['target_device'] as $v2){
                        foreach ($v['schedule_time'] as $v3){
                            $save_schedule = [
                                'schedule_value'=>implode(',',$v3['time']),
                                'schedule_type'=>$v['schedule_type'],
                                'push_num'=>$v3['push_num'],
                                'target_device'=>$v2,
                                'task_id'=>$task['id'],
                                'user_id'=>$task['user_id'],
                                'platform'=>$v['platform'],
                                'status'=>0,
                                'is_auto_jjjb'=>$task['is_auto_jjjb'],
                                'is_auto_fbjb'=>$task['is_auto_fbjb'],
                                'run_script'=>json_encode($run_script)
                            ];
                            $this->app->db->name('redian_push_schedule')->insert($save_schedule);
                        }
                    }
                }
            }
        }
        RedianTask::mQuery()->where([ "id"=>$task['id']])->inc('count')
            ->update([
                'error_status'=>0,
                'progress'=>0,
                'status'=>3,
                'last_push'=>date('Y-m-d H:i:s')
            ]);

        if($task['type']==1){
            $this->app->db->name("task_run_log")->where(['id'=>$task_run_log_id])->update(['error_status'=>0,'result'=>null]);
            $code = sysqueue($task['title']."热点转换任务", "xbase:TaskRunV2", $later = 0, $data = [
                'task_id'=>$task_id,'task_run_log_id'=>$task_run_log_id
            ], $rscript = 1, $loops = 0);

        }else{
            $code = sysqueue($task['title']."对标账号转换任务", "xbase:TaskRunAccount", $later = 1, $data = [
                'task_id'=>$task['id'],'task_run_log_id'=>$task_run_log_id
            ], $rscript = 1, $loops = 0);
        }
    }

    //开始任务
    public function startTask()
    {

        $task_id = input('task_id');
        $status = input('status',1);
        $task = RedianTask::mk()->where(['user_id'=>$this->user_id,"id"=>$task_id])->find();
        $error_status = $task['error_status'];
        if($task['error_status']==0 && ($task['progress']!=0  && $task['progress']!=7 && $task['progress']!=6 )){
            $this->error('任务正在执行中,请等待任务执行完成后再试');

        }
        if($task['type']==1 ||  $task['type']==4){
            switch ($status){
                case 1:
                    $this->runTask($task_id);
                    break;
                case 0:
                    RedianTask::mQuery()->where(['user_id'=>$this->user_id,"id"=>$task_id])->update(['status'=>0]);
                    break;
                case 2:
                    $task_run_log_id = input('task_run_log_id',0);
                    $error_status = $this->app->db->name("task_run_log")->where(['id'=>$task_run_log_id])->value('error_status');
                    RedianTask::mQuery()->where(['user_id'=>$this->user_id,"id"=>$task_id])->update(['progress'=>$error_status,'error_status'=>0,'status'=>3]);
                    if($task['type']==1){
                        $this->app->db->name("task_run_log")->where(['id'=>$task_run_log_id])->update(['error_status'=>0,'result'=>null]);
                        $code = sysqueue($task['title']."热点转换任务", "xbase:TaskRunV2", $later = 0, $data = [
                            'task_id'=>$task_id,'task_run_log_id'=>$task_run_log_id
                        ], $rscript = 1, $loops = 0);

                    }else{
                        $this->app->db->name("task_run_log")->where(['id'=>$task_run_log_id])->update(['error_status'=>0,'result'=>null]);
                        $code = sysqueue($task['title']."对标账号转换任务", "xbase:TaskRunAccount", $later = 1, $data = [
                            'task_id'=>$task['id'],'task_run_log_id'=>$task_run_log_id
                        ], $rscript = 1, $loops = 0);
                    }


                    break;
            }
        }elseif($task['type']==2){
            RedianTask::mQuery()->where(['user_id'=>$this->user_id,"id"=>$task_id])->update(['progress'=>$error_status,'error_status'=>0]);
            $code = sysqueue($task['title']."热点转换任务", "xbase:taskRun", $later = 0, $data = [
                'task_id'=>$task_id,
            ], $rscript = 1, $loops = 0);
        }else{
            RedianTask::mQuery()->where(['user_id'=>$this->user_id,"id"=>$task_id])->update(['progress'=>$error_status,'error_status'=>0]);
            $code = sysqueue($task['title']."热点转换任务", "xbase:TaskRunUrl", $later = 0, $data = [
                'task_id'=>$task_id,
            ], $rscript = 1, $loops = 0);
        }


        $this->success("操作成功");



        $task = RedianTask::mQuery()->where(['user_id'=>$this->user_id,"id"=>$task_id])->find();
        TaskService::instance()->videoStatus($task);exit;
        $redian_data = TaskService::instance()->videoGenerate($task);
        if(empty($redian_data)){
            $this->error('任务执行失败:视频生成失败');
            RedianTask::mQuery()->where(['user_id'=>$this->user_id,"id"=>$task_id])->update(['progress'=>7]);
        }
        exit;
        $redian_data = TaskService::instance()->textGenerate($task);exit;
        if(empty($task)){
            $this->error('任务不存在');
        }
        //开始采集任务
        dump("开始采集任务");
        RedianTask::mQuery()->where(['user_id'=>$this->user_id,"id"=>$task_id])->update(['progress'=>1]);
        $redian_data = TaskService::instance()->startGather($task);
        if(empty($redian_data)){
            $this->error('任务执行失败:采集失败');
            RedianTask::mQuery()->where(['user_id'=>$this->user_id,"id"=>$task_id])->update(['progress'=>7]);
        }

        //计算相似度
        dump("计算相似度");
        RedianTask::mQuery()->where(['user_id'=>$this->user_id,"id"=>$task_id])->update(['progress'=>2]);
        $redian_data = TaskService::instance()->similarity($task);
        if(empty($redian_data)){
            $this->error('任务执行失败:相似度计算失败');
            RedianTask::mQuery()->where(['user_id'=>$this->user_id,"id"=>$task_id])->update(['progress'=>7]);
        }
        $out_redian_data = RedianData::mQuery()->where(['task_id'=>$task_id])->order('similarity desc')->limit(0,$task['out_num'])->select()->toArray();
        $out_redian_data_ids = array_column($out_redian_data,'id');
        RedianData::mQuery()->whereIn('id',$out_redian_data_ids)->update(['filter_status'=>1]);
        //口播文本生成
        dump("口播文本生成");
        RedianTask::mQuery()->where(['user_id'=>$this->user_id,"id"=>$task_id])->update(['progress'=>3]);
        $redian_data = TaskService::instance()->textGenerate($task);
        if(empty($redian_data)){
            $this->error('任务执行失败:口播文本生成失败');
            RedianTask::mQuery()->where(['user_id'=>$this->user_id,"id"=>$task_id])->update(['progress'=>7]);
        }
        //音频生成
        dump("音频生成");
        RedianTask::mQuery()->where(['user_id'=>$this->user_id,"id"=>$task_id])->update(['progress'=>4]);
        $redian_data = TaskService::instance()->audioGenerate($task);
        if(empty($redian_data)){
            $this->error('任务执行失败:音频生成失败');
            RedianTask::mQuery()->where(['user_id'=>$this->user_id,"id"=>$task_id])->update(['progress'=>7]);
        }
        //视频生成
        dump("视频生成");
        RedianTask::mQuery()->where(['user_id'=>$this->user_id,"id"=>$task_id])->update(['progress'=>5]);
        $redian_data = TaskService::instance()->videoGenerate($task);
        if(empty($redian_data)){
            $this->error('任务执行失败:视频生成失败');
            RedianTask::mQuery()->where(['user_id'=>$this->user_id,"id"=>$task_id])->update(['progress'=>7]);
        }
        //创建视频生成状态查询任务
        $this->success('操作成功', $redian_data);




    }

    //删除任务
    public function delTask()
    {
        $task_id = input('task_id');
        $task = RedianTask::mQuery()->where(['user_id'=>$this->user_id,"id"=>$task_id,'is_deleted'=>0])->find();
        if(empty($task)){
            $this->error('任务不存在');
        }
        if($task['error_status']==0 && ($task['progress']!=0  && $task['progress']!=7 && $task['progress']!=6 )){
            $this->error('任务正在执行中,请等待任务执行完成后再试');

        }
        RedianTask::mQuery()->where(['user_id'=>$this->user_id,"id"=>$task_id])->update(['is_deleted'=>1]);
        RedianData::mQuery()->where(['task_id'=>$task_id])->update(['is_deleted'=>1]);
        $this->success('操作成功');
    }


    //任务详情
    public function getInfo()
    {
        $task_id = input('task_id');
        $where = ['user_id'=>$this->user_id,"id"=>$task_id];

        $task = RedianTask::mQuery()->where($where)->find();
        if(empty($task)){
            $this->error('任务不存在');
        }
        // 将多行文本按换行符分割成数组
        $remarkLines = explode("\n", $task['keywords']);
        // 去除每一行的前后空格
        $remarkLines = array_map('trim', $remarkLines);
        // 去除空行
        $remarkLines = array_filter($remarkLines, function($line) {
            return !empty($line);
        });
        $task['keywords'] = $remarkLines;
        if($task['push_schedule']){
            $task['push_schedule_arr'] = json_decode($task['push_schedule'],true);
            $target_devices = [];
            $task['target_platform'] = "";
            $task['push_num'] = "";
            foreach ($task['push_schedule_arr'] as $v){
                foreach ($v['target_device'] as $v1){
                    $target_devices[] = $v1;
                }
                if(is_array($v['platform'])){
                    $target_platform = [];
                    foreach ($v['platform'] as $v2){
                        switch ($v2){
                            case 1:
                                $target_platform[] = "抖音";
                                break;
                            case 2:
                                $target_platform[] = "小红书";
                                break;
                            case 3:
                                $target_platform[] = "视频号";
                                break;
                        }
                    }
                    $task['target_platform'] = implode(',',$target_platform);
                }else{
                    switch ($v['platform']){
                        case 1:
                            $task['target_platform'] = "抖音";
                            break;
                        case 2:
                            $task['target_platform'] = "小红书";
                            break;
                        case 3:
                            $task['target_platform'] = "快手";
                            break;
                    }
                }


                $task['push_num'] = $v['push_num'];
            }
            $target_devices = $this->app->db->name('ai_device')->whereIn('id',$target_devices)->column('device_name');
            $task['target_devices'] = implode(',',$target_devices);
        }else{
            $task['push_schedule_arr'] = [];
        }
        if($task['schedule_task']){
            $task['schedule_task_arr'] = json_decode($task['schedule_task'],true);
        }
        //查询音频模板名称
        $task['voice_name'] = $this->app->db->name('ai_voice')->where(['id'=>$task['voice']])->value('name');
        //查询视频模板名称
        $task['video_name'] = $this->app->db->name('ai_voice')->where(['id'=>$task['video']])->value('name');


        $this->success('操作成功', $task);
    }

    //设置任务状态
    public function setTaskStatus()
    {
        $task_id = input('task_id');
        $status = input('status');
        $task = RedianTask::mQuery()->where(['user_id'=>$this->user_id,"id"=>$task_id])->find();
        if(empty($task)){
            $this->error('任务不存在');
        }
        RedianTask::mQuery()->where(['user_id'=>$this->user_id,"id"=>$task_id])->update(['status'=>$status]);
        $this->success('操作成功');
    }

    //获取任务热点数据
    public function getData()
    {
        $task_id = input('task_id');
        $task_run_log_id = input('task_run_log_id',0);
        $where = ["task_id"=>$task_id];
        if($task_run_log_id){
            $where['task_run_log_id'] = $task_run_log_id;
        }
        $page= input('page',1);
        $page_size = input('page_size',20);
        $redian_data = RedianData::mQuery()->where($where)
            ->order('filter_status desc,similarity desc,time desc,id desc')->page($page,false,false,$page_size);
        foreach ($redian_data['list'] as &$v){
            $v['time'] = date('Y-m-d H:i:s',$v['time']);
            $v['description'] = strip_tags(htmlspecialchars_decode($v['description']));
            $v['device_name'] = $this->app->db->name('ai_device')->where(['id'=>$v['schedule_id']])->value('device_name');
        }
        $this->success('操作成功', $redian_data);
    }

    //获取定时任务执行次数日志
    public function getTaskRunLog()
    {
        $task_id = input('task_id');
        $task_run_log = $this->app->db->name('task_run_log')->where(['task_id'=>$task_id])->select()->toArray();
        $this->success('操作成功', $task_run_log);
    }

    //任务数据统计
    public function getDataCount()
    {
        //成功转化数量
        $task_id = input('task_id');
        $total = RedianData::mQuery()
            ->where([
                'task_id'=>$task_id,
            ])
            ->count();
        $aigaixu_count = RedianData::mQuery()
            ->where([
                'task_id'=>$task_id,
                'gaixie_status'=>2,
            ])->whereNotNull("ai_gaixie_content")
            ->count();
        $audio_count = RedianData::mQuery()
            ->where([
                'task_id'=>$task_id,
                'audio_status'=>3,
            ])->whereNotNull("audio_url")
            ->count();
        $success_count = RedianData::mQuery()
            ->where([
                'task_id'=>$task_id,
                'video_status'=>3,
            ])->whereNotNull("video_url")
            ->count();
        $this->success('操作成功', ['aigaixu_count'=>$aigaixu_count,'audio_count'=>$audio_count,'success_count'=>$success_count,'total'=>$total]);
    }
    //批量下载视频
    public function batchDownloadVideo()
    {
        $data = input();
        $code = sysqueue("批量下载视频", "xbase:BatchDownloadVideo", $later = 1, $data = [
            'task_id'=>$data['task_id'],'task_run_log_id'=>$data['task_run_log_id']
        ], $rscript = 1, $loops = 0);
        $this->success('操作成功', $code);
    }

    //获取视频压缩包地址
    public function getVideoZipUrl()
    {
        $task_id = input('task_id');
        $task_run_log_id = input('task_run_log_id',0);
        if($task_run_log_id){
            $zip_url = $this->app->db->name("task_run_log")->where(["id"=>$task_run_log_id])->value("zip_url");
        }else{
            $zip_url = RedianTask::mQuery()->where(['user_id'=>$this->user_id,"id"=>$task_id])->value("zip_url");
        }


        if(empty($zip_url)){
            $this->error('任务未完成');
        }
        $this->success("success",$zip_url);
    }

    //手动选择文案完成,重启任务
    public function manualComplete(){
        $data = $this->app->request->post();
        $task_id = $data['task_id'];
        $task_run_log_id = $data['task_run_log_id'];
        $task = RedianTask::mQuery()->where(['user_id'=>$this->user_id,"id"=>$task_id])->find();
        if(empty($task)){
            $this->error('任务不存在');
        }
        RedianTask::mQuery()->where(['user_id'=>$this->user_id,"id"=>$task_id])->update(['progress'=>3]);
        RedianData::mQuery()->whereIn('id',$data['ids'])->update(['filter_status'=>1]);
        $code = sysqueue($task['title']."热点转换任务(手动选择文案完成)", "xbase:TaskRunV2", $later = 1, $data = [
            'task_id'=>$task['id'],'task_run_log_id'=>$task_run_log_id
        ], $rscript = 1, $loops = 0);
        $this->success('操作成功', $code);
    }


    //手动推送视频到手机
    public function manualPushVideo()
    {
        $parms = $this->app->request->post();
        $ids = $parms['ids'];
        $devices =$parms['device_ids'];
        $data =  RedianData::mQuery()->whereIn('id',$ids)->select()->toArray();

        // 循环设备列表：当索引超出时重置为0
        $deviceIndex = 0;
        $deviceCount = count($devices);
        $platform = $parms['platform'];
       // dump($platform);
        if($parms['is_auto_jjjb']==1){
            $run_script[]=$this->app->db->name("script_confg")->where('type',0)->value('name');
        }
        if($parms['is_auto_fbjb']==1){
            $fb_jb=$this->app->db->name("script_confg")->whereIn('type',implode(",",$platform))->column('name');
            foreach ($fb_jb as $item){
                $run_script[]=$item;
            }
        }
       // $this->success('操作成功',$run_script);

        $this->app->db->name("redian_data")->whereIn('id',$ids)->update([
            'is_auto_jjjb'=>$parms['is_auto_jjjb'],
            'is_auto_fbjb'=>$parms['is_auto_fbjb']
        ]);
        $info = "";
        $error_title=[];
        //循环数据推送到对应设备上,按顺序取出设备,当取到最后一个设备时,重新开始循环取设备
        foreach ($data as $item) {
            // 获取当前分配的设备ID（循环使用）
            $target_device = $devices[$deviceIndex];
            $this->app->db->startTrans();
            //获取对应平台执行脚本

            try {
                //获取对应平台执行脚本
                if(isset($run_script[0])){
                    $script_file = $run_script[0];
                    $next_script_file ="";
                    if(isset($run_script[1])){
                        $next_script_file = $run_script[1];
                    }
                    $content = [
                        'type'=>1,
                        'script_files'=>$run_script,
                        'script_file'=>$script_file,
                        'next_script_file'=>$next_script_file,
                        'data_id'=>$item['id'],
                        'is_update'=>1
                    ];
                    WsSendService::instance()->send($target_device,1,$content);
                    //将设备设置成繁忙
                    $this->app->db->name("ai_device")->where(['id'=>$target_device])->update([
                        'work_status'=>2,
                        'last_run_time' => date('Y-m-d H:i:s')
                    ]);
                    // 更新推送次数及数据推送的设备 平台
                    $this->app->db->name("redian_data")->where('id',$item['id'])->inc('push_num',1)->update([
                        'is_push'=>1,
                        'schedule_id'=>$target_device,
                        'platform'=>$platform,
                        'run_script'=>json_encode($run_script),
                        'push_time'=>date('Y-m-d H:i:s'),
                    ]);

                    // 提交事务
                    $this->app->db->commit();
                    $info = 1;
                }
            } catch (\Exception $e) {
                $this->app->db->rollback();
                $info = $e->getMessage();
                $error_title[]=$item['video_title'];
                //  throw $e;
            }
            // 更新设备索引（循环）
            $deviceIndex = ($deviceIndex + 1) % $deviceCount;
        }
        if($error_title){
            $this->error(implode(",",$error_title)."视频推送失败");
        }else{
            $this->success('操作成功');
        }

    }

    public function getRunLog()
    {
        $task_id = input('task_id');
        $id = input('id');
        //$task_run_log_id = input('task_run_log_id');
        $content = $this->app->db->name("task_log")->where(['task_id'=>$task_id])
            ->where('id','>',$id)
            ->order('id desc')->find();
        $this->success('success',$content);

    }



    //以下是测试内容非实际接口

    //开始采集任务
    public function startGather()
    {
        $task_id = input('task_id');
        $task = RedianTask::mk()->find($task_id);
        if(empty($task)){
            $this->error('任务不存在');
        }
        $num = $task['num'];
        $pages = ceil($num/50);
        $list = [];
        for($i=1;$i<=$pages;$i++){
            $res = TophubdataService::instance()->search($this->user_id,$task['keywords'],$i);
           // dump($res);
            if($res['code']==0){
                $this->error($res['msg']);
            }else{
               // $res = $res['data'];
                $list = array_merge($list,$res['data']['items']);
            }
        }
        foreach ($list as $k=>$v){
            $data = [
                'task_id'=>$task_id,
                'title'=>$v['title'],
                'url'=>$v['url'],
                'description'=>$v['description'],
                'thumbnail'=>$v['thumbnail'],
                'extra'=>$v['extra'],
                'time'=>$v['time'],
            ];
            RedianData::mk()->insert($data);
        }
        $this->success('获取成功', $list);

    }

    //口播文本生成
    public function textGenerate()
    {
        $data = $this->app->request->post();
        $redian_data = RedianData::mQuery()->whereIn('id',$data['redian_ids'])->select()->toArray();
        foreach ($redian_data as &$v){
            $user_content = str_replace( ['{{标题}}', '{{描述}}'], [$v['title'], $v['description']], $data['user_content']);
            $system_content = $data['system_content'];
            $res = AlibailianService::instance()->createText($this->user_id,$system_content,$user_content);
            if(isset($res['choices'][0]['message']['content'])){
                $v['ai_gaixie_content'] = $res['choices'][0]['message']['content'];
                $v['gaixie_status'] = 2;
            }else{
                $v['ai_gaixie_content'] = '改写失败:'.$res['message'];
                $v['gaixie_status'] = 3;
            }
            RedianData::mk()->where(['id'=>$v['id']])->save($v);
        }
        $this->success('操作成功', $redian_data);


    }

    //音频生成
    public function audioGenerate()
    {
        $data = $this->app->request->post();
        $redian_data = RedianData::mQuery()->whereIn('id',$data['redian_ids'])->select()->toArray();
        foreach ($redian_data as &$v){
            if(empty($v['ai_gaixie_content'])){
                $v['audio_errmsg'] = "口播文本未生成";
                $v['audio_status'] = 4;
            }else{
                $res = HuoshanService::instance()->createAudio($this->user_id,$v['ai_gaixie_content']);
                if(isset($res['task_id'])){
                    $v['audio_id'] = $res['task_id'];
                    $v['audio_status'] = 2;
                    $v['audio_errmsg'] = null;
                }else{
                    $v['audio_errmsg'] = $res['message'];
                    $v['audio_status'] = 4;
                }
            }

            RedianData::mk()->where(['id'=>$v['id']])->save($v);
        }
        $this->success('操作成功', $redian_data);
    }

    //查询音频状态
    public function audioStatus()
    {
        $data = $this->app->request->post();
        $redian_data = RedianData::mQuery()->whereIn('id',$data['redian_ids'])->select()->toArray();
        $this->success('操作成功', $redian_data);
    }



    //视频生成
    public function videoGenerate()
    {
        $data = $this->app->request->post();
        $redian_data = RedianData::mQuery()->whereIn('id',$data['redian_ids'])->select()->toArray();
        foreach ($redian_data as &$v){
            if(empty($v['audio_url'])){
                $v['video_errmsg'] = "口播音频未生成";
                $v['video_status'] = 4;
            }else{
                $res = HeygenService::instance()->createVideo($this->user_id,$v['audio_url']);
               // dump($res);
                if(isset($res['data']['video_id'])){
                    $v['video_id'] = $res['data']['video_id'];
                    $v['video_status'] = 2;
                    $v['video_errmsg'] = null;
                    $v['video_url'] = null;
                    $v['video_url_caption'] = null;
                    $v['video_data'] = null;

                }else{
                    $v['video_errmsg'] = $res['error']['message'];
                    $v['video_status'] = 4;
                }
            }

            RedianData::mk()->where(['id'=>$v['id']])->save($v);

        }
        $this->success('操作成功', $redian_data);
    }

    //查询视频状态
    public function videoStatus()
    {
        $data = $this->app->request->post();
        $redian_data = RedianData::mQuery()->whereIn('id',$data['redian_ids'])->select()->toArray();
        foreach ($redian_data as &$v){
            if(!empty($v['video_id'])){
                $res = HeygenService::instance()->getVideoStatus($this->user_id,$v['video_id']);
                dump($res);exit;
                if(isset($res['data']['video_url'])){
                    $v['video_url'] = $res['data']['video_url'];
                    $v['video_status'] = 3;
                    $v['video_errmsg'] = null;
                    $v['video_url_caption'] = $res['data']['video_url_caption'];
                    $v['video_data'] = json_encode($res['data']);
                    RedianData::mk()->where(['id'=>$v['id']])->save($v);
                }else{
                    $v['video_heygen_data'] = $res;
                }
            }
        }
        $this->success('操作成功', $redian_data);
    }

    //查询Heygen上的视频模板列表
    public function getVideoTemplateList()
    {
        $list = HeygenService::instance()->getVideoTemplate($this->user_id);
        $this->success('获取成功', $list);
    }


    //Minimax音频生成
    public function minimaxAudioGenerate()
    {
        $data = $this->app->request->post();
        $redian_data = RedianData::mQuery()->whereIn('id',$data['redian_ids'])->select()->toArray();
        foreach ($redian_data as &$v){
            if(empty($v['ai_gaixie_content'])){
                $v['audio_errmsg'] = "口播文本未生成";
                $v['audio_status'] = 4;
            }else{
                $res = MinimaxService::instance()->createVoiceFile($this->user_id,$v['ai_gaixie_content']);
                $this->success("success", $res);
            }

        }
    }
    //Minimax音频状态查询
    public function minimaxAudioStatus()
    {
       $task_id = input('task_id');
        $res = MinimaxService::instance()->getVoiceStatus($this->user_id,$task_id);
        dump($res);
    }
    //Minimax音频下载地址
    public function getminimaxAudio()
    {
        $file_id = input('file_id');
        $res = MinimaxService::instance()->getVoice($this->user_id,$file_id);
        dump($res);
    }

    //影刀任务数据
    public function getTaskData()
    {
        $list = RedianTask::mk()->where(['user_id'=>$this->user_id,'yd_is'=>0,'progress'=>6])
            ->field("id,user_id,title,code,keywords")
            ->order('id desc')->find();
        $data = RedianData::mk()->field("title,video_url")->whereNotNull('video_url')->where(['task_id'=>$list['id']])->select()->toArray();
        $this->success('获取成功', $data);

    }


    //相似度计算
    public function similarity()
    {
        $task_id = input('id');
        dump($task_id);
        $keywords = RedianTask::mQuery()->where(['id'=>$task_id])->value('keywords');
        dump($keywords);
        $redian_data = RedianData::mQuery()->field("title,description")->where(['task_id'=>$task_id])->limit(0,5)->select()->toArray();
        foreach ($redian_data as &$v){
            $content = $v['title'];
            if($v['description']){
                //清除html代码
                dump($v['description']);
                $content.=strip_tags(htmlspecialchars_decode($v['description']));
                dump($content);
            }
            $v['similarity'] = SimilarityService::instance()->similarity($keywords,$content);
        }
        dump($redian_data);

    }


    public function cmp()
    {
        dump($this->app->request->domain().'/mp3/audio.mp3');exit;


    }


    //获取热点数据详情
    public function getHotData()
    {
        $id = input('id');
        $data = RedianData::mk()->where(['id'=>$id])->find();
        RedianData::mk()->where(['id'=>$id])->save(['is_receive'=>1]);
        $this->success('获取成功', $data);

    }

    public function updateData()
    {
        $data = $this->app->request->get();
        RedianData::mk()->where(['id'=>$data['id']])->save($data);
        $this->success('更新成功');
    }








}
